package samples.online;

import java.net.URL;

import de.fmui.spheon.jsoap.Envelope;
import de.fmui.spheon.jsoap.Fault;
import de.fmui.spheon.jsoap.ParamBag;
import de.fmui.spheon.jsoap.RPCCall;
import de.fmui.spheon.jsoap.SoapConfig;
import de.fmui.spheon.jsoap.transport.http.SoapHttpClient;

/**
 * This class is a client of a webserivce of Spehon JSOAP. 
 * (See http://soap.fmui.de/webservices.php)
 * It gets information about elements from the periodic table.
 *
 * To run this client make sure that you have a XML-Parser (e.g. Xerces),
 * spheon-jsoap.jar, this class and the class ChemistryElement in your classpath. 
 */
public class ChemistryClient
{
	private boolean numberOrSymbol;
	private int		number;
	private String	symbol;
	private URL		proxy;
	
	public ChemistryClient(String s, URL proxy)
	{
		this.proxy = proxy;
		
		try {
			number = Integer.parseInt(s.trim());
			numberOrSymbol = true;
		}
		catch(Exception e) {
			symbol = s.trim();
			numberOrSymbol = false;			
		}
		
		doIt();
	}
	
	public void doIt()
	{
		long startTime;
		long processTime;
		long endTime;
		
		// get configuration
		SoapConfig sc = new SoapConfig();
		SoapConfig.Xsd xsd = sc.addNamespace("http://spheon.bov.de");
		sc.addStructType(xsd, "ChemistryElement", "samples.online.ChemistryElement");
		
		String method;
		if(numberOrSymbol)	{ method = "getElementByNumber"; }
		else				{ method = "getElementBySymbol"; }
		
		// prepare a call
		RPCCall call = new RPCCall(sc, "urn:SpheonJSOAPChemistry", method);

		try {
			
			// set parameter
			if(numberOrSymbol)	{ call.setParameter("number", new Integer(number), int.class); } 
			else 				{ call.setParameter("symbol", symbol, String.class); }
	
			// execute call (via HTTP)
		    SoapHttpClient shc = new SoapHttpClient(sc, proxy);
		    startTime = System.currentTimeMillis();	
    
			Envelope answer = shc.invokeHTTP(new URL("http://soap.fmui.de/RPC"), "", call.getEnvelope()); 

			processTime = System.currentTimeMillis();

				
			// check for a fault message
			if(answer.hasFault()) {			
				Fault f = answer.getFault();
				System.out.println("Faultcode:   " + f.getFaultcode());
				System.out.println("Faultstring: " + f.getFaultstring());
				System.out.println("Faultactor:  " + f.getFaultactor());		
				return;
			}
		
			// get the result
			ParamBag p = call.getResults(answer);
			
			ChemistryElement ce = (ChemistryElement) p.getValue(0);
			
			endTime = System.currentTimeMillis();
			
			System.out.println("-----------------------------------------");
			System.out.println("Atomic number: " + ce.atomicnumber);
			System.out.println("Symbol:        " + ce.symbol);
			System.out.println("Name:          " + ce.name);
			System.out.println("Mass:          " + ce.mass);
			System.out.println("Melting point: " + ce.meltingPoint + " K");			
			System.out.println("Boiling point: " + ce.boilingPoint + " K");			
			System.out.println("Found:         " + ce.found);						
			System.out.println("-----------------------------------------");
			System.out.print("Processing Time: ");
			System.out.println(processTime - startTime);
			System.out.print("Total time:      ");
			System.out.println(endTime - startTime);
		}
		catch(Exception e) {
			System.out.println("Exception: " + e);
			e.printStackTrace();
		}	
	}
	
	public static void main(String[] args) 
	{
		if(args.length < 1) {
			System.out.println("ChemistryClient <atomicnumber|symbol> [proxy server 'IP:port']");
			System.exit(1);
		}

		URL	proxy = null;
		if(args.length > 1) { 
			try {
				int sep 		= args[1].indexOf(":");		
				String server 	= args[1].substring(0, sep);
				int port 		= Integer.parseInt(args[1].substring(sep+1));;
				proxy 			= new URL("http", server, port, "/");
			}	
			catch(Exception e) {
				System.out.println("Invalid proxy server.");
				System.exit(1);
			}
		} 
		
		new ChemistryClient(args[0], proxy);
	}		
}